package cn.zhiyong.pr.addItem;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JTextPane;
import javax.swing.JToggleButton;
import javax.swing.event.MouseInputAdapter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.undo.UndoManager;

import cn.zhiyong.pr.main.MyConstant;

public class ContentPanel_controller extends MouseInputAdapter implements
		ItemListener, ActionListener{

	private ContentPanel view;
	private ContentPanel_bean bean;
	private UndoManager undomanager;

	public ContentPanel_controller(ContentPanel contentPanel,
			ContentPanel_bean bean) {
		this.view = contentPanel;
		this.bean = bean;
		this.undomanager = new UndoManager();
	}

	public static boolean hasFont(String[] fontNames, String aFontName) {
		boolean rt = false;
		for (String fn : fontNames) {
			if (fn.equals(aFontName)) {
				rt = true;
				break;
			}
		}
		return rt;
	}

	public void setUIEnableToBean(boolean entryEditable) {
		bean.setAllEditable(entryEditable);
	}

	public void clearContent() {
		bean.clearDocument();
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		Object o = e.getSource();
		if (o instanceof JComboBox) {
			JComboBox cb = (JComboBox) o;
			if (cb.getName().equals("font_CBB")) {
				String fontName = (String) cb.getSelectedItem();
				view.update_fontFamily(fontName);
			}
			if (cb.getName().equals("fontSize_CBB")) {
				int size = (Integer) cb.getSelectedItem();
				view.update_fontSize(size);
			}

		}

		view.updateUIFromData();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object o = e.getSource();
		if (o instanceof JToggleButton) {
			JToggleButton tgbt = (JToggleButton) o;
			if (tgbt.getName().equals(MyConstant.font_bold)) {
				view.update_fontBold(tgbt.isSelected());
			}
			if (tgbt.getName().equals(MyConstant.font_italic)) {
				view.update_fontItalic(tgbt.isSelected());
			}
			if (tgbt.getName().equals(MyConstant.font_underline)) {
				view.update_fontUnderline(tgbt.isSelected());
			}
		}

		if (o instanceof JButton) {
			JButton bt = (JButton) o;
			if (bt.getName().equals(MyConstant.undo)) {
				undomanager.undo();
			} else if (bt.getName().equals(MyConstant.redo)) {
				undomanager.redo();
			} else if (bt.getName().equals(MyConstant.addPic)) {
				JFileChooser fc = new JFileChooser();
				fc.setFileFilter(new FileNameExtensionFilter("ͼƬ", "jpg",
						"bmp", "gif", "png"));
				int result = fc.showOpenDialog(view);
				if (result == JFileChooser.APPROVE_OPTION) {
					File f = fc.getSelectedFile();
					view.addPic(f);
				}
			}
		}

		view.updateUIFromData();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		Object o = e.getSource();
		if (o instanceof JTextPane) {
			JTextPane tp = (JTextPane) o;
			if (tp.getName().equals("txtPane")) {
				view.update_fontStyle_components_based_on_selection();
			}
		}

		view.updateUIFromData();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		Object o = e.getSource();
		if (o instanceof JTextPane) {
			JTextPane tp = (JTextPane) o;
			if (tp.getName().equals("txtPane")) {
				view.update_fontStyle_components_based_on_caret();
			}
		}

		view.updateUIFromData();
	}

	public UndoManager getUndoManager() {
		return this.undomanager;
	}

}
